﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using StockCommon.baseclass;

namespace StockPostProcessLib.source.LastTradeDay
{
    /**********************************************
     *   最後資料日期
     * 
     * *********************************************/
    public class LastTradeUpdate:DBAccess
    {
        protected class CodeTime
        {
            public String code { get; set; }
            public DateTime time { get; set; }

        }
        public static void update()
        {
            var context = getContext();
            Dictionary<String, CodeTime> codeTimes = context.ExecuteQuery<CodeTime>("select StockDaily.證券代號 as code, MAX(日期) as time from StockDaily group by StockDaily.證券代號 order by 證券代號").ToDictionary(x => x.code);

            var recs = (from si in context.StockIndexes select si);
            foreach (var rec in recs)
            {
                String code = rec.代號;
                if (codeTimes.ContainsKey(code))
                {
                    rec.最後資料日期 = codeTimes[code].time;
                }
            }
            context.SubmitChanges();
        }
    }
}
